# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   Auto_api_model
# FileName:      read_excel.py
# Author:       xgy
# Datetime:     2025/9/13 09:29
# Description:
# ---------------------------------------------------------------------------
import os.path
from time import sleep

import openpyxl
from openpyxl.reader.excel import load_workbook

from ApiAutoTest.reader_utils.read_ini import Ini
from ApiAutoTest.reader_utils.read_json import read_json


class ReadExcel:
    def __init__(self):
        # 获取excel文件路径，和工作簿,工作表
        self.ini =Ini()
        excel_path = self.ini.get_file_path("excel")
        excel_table_name = self.ini.get_table("name")
        try:
            wb = openpyxl.load_workbook(excel_path)
            # 获取工作表
            self.ws = wb[excel_table_name]
        except Exception as e :
            # todo 记录日志
            raise e
    # 获取每个单元格的值
    def __get_cell_value(self,col,row):
        try:
            cell_value = self.ws[col+str(row)].value
        except Exception as e:
            raise e
        else:
            if isinstance(cell_value,str) and len(cell_value.strip())>0:
                return cell_value.strip()

    # 获取到接口的请求方法
    def req_method(self,row):
        return self.__get_cell_value("f",row)

    # 获取接口路径
    def req_url(self,row):
        path = self.__get_cell_value("g",row)

        if path:
            host  = self.ini.get_host("bpm")
            return host + path
    # 获取到媒体类型
    def req_mine(self,row):

        mine = self.__get_cell_value("h",row)

        if mine:
            return mine.lower()

    # 获取模块名称
    def module_name(self,row):
        return self.__get_cell_value("b",row)

    #获取接口名称
    def api_name(self,row):
        return self.__get_cell_value("c",row)

    def req_data(self,row):

        module = self.module_name(row)
        api = self.api_name(row)

        data_key = self.__get_cell_value("i",row)

        if module and api and data_key :
            #获取到python字典
            case_data_path = self.ini.get_file_path("case")

            case_data = read_json(case_data_path)

            return case_data[module][api][data_key]


    def expect_data(self,row):
        module = self.module_name(row)
        api = self.api_name(row)

        expect_data_key =  self.__get_cell_value("j",row)

        if module and api and expect_data_key:

            expect_data_path  = self.ini.get_file_path("expect")
            expect_data = read_json(expect_data_path)

            return expect_data[module][api][expect_data_key]
    # 获取二维列表得用例数据
    def get_data(self):
        list_data = []

        for row  in range(2,self.ws.max_row+1):
            case_method = self.req_method(row)
            case_url = self.req_url(row)
            case_mine = self.req_mine(row)
            case_data = self.req_data(row)
            expect_data = self.expect_data(row)
            # 过滤掉空行得数据
            if case_method and case_url and expect_data:
                list_data.append([case_method,case_url,case_mine,case_data,expect_data])
        else:
            return list_data



if __name__ == '__main__':
    excel = ReadExcel()
    # print(excel.get_data())
    for i in excel.get_data():
        print(i)
